package com.budwk.app.util.poiUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.budwk.app.sqfw.dto.YuHongMenStreetFireHiddenCheckWordDTO;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.*;

/**
 * @author yangweimin
 * @since 2023/10/9
 *
 * <p>
 * 宇虹社区消防安全文档生成工具类
 * </p>
 */
public class YuHongMenStreetFireHiddenOfficeUtils {
    /**
     * 生成宇虹社区消防的安全排查pdf
     *
     * @param formSummary 数据DTO
     * @return pdf生成结果
     */
    public static GenerateWordAndPdfUrlVO createFireHiddenPDF(YuHongMenStreetFireHiddenCheckWordDTO formSummary) {

        List<ByteArrayOutputStream> osList = Lists.newArrayList();

        // 经营场所消防安全检查记录表
        osList.add(businessSiteFireSafety(formSummary));

        // 瓶装燃气用户安全检查表
        if (formSummary.getA6().contains("液化石油气") || formSummary.getA6().contains("城镇燃气用二甲醚")) {
            osList.add(bottledGasUserSafety(formSummary));
        }

        // 现场照片
        osList.add(createSitePicture(formSummary));

        //String fileName = StrFormatter.format("{}安全排查整治表{}", formSummary.getA1(), DateUtil.format(new Date(), "yyyyMMdd"));
        //zhangp 2023-12-13 文件名称格式统一
        String downloadFileName = DownLoadFileNameEnum.SAFETY_INVESTIGATE_RENOVATE.getName();
        String fileName = StrFormatter.format("{}{}{}", formSummary.getA1(), downloadFileName, DateUtil.format(new Date(), "yyyyMMdd"));

        return wordToPdfAndUpload(osList, fileName, formSummary.getUnitName() + "消防安全走访巡查表");
    }

    /**
     * 下载模板
     * @param formSummary
     * @return
     */
    public static GenerateWordAndPdfUrlVO downTemplate(YuHongMenStreetFireHiddenCheckWordDTO formSummary) {

        List<NiceXWPFDocument> documents = new ArrayList<>();

        documents.add(businessSiteFireSafetyTemplate(formSummary));
        documents.add(bottledGasUserSafetyTemplate(formSummary));

        GenerateWordAndPdfUrlVO vo = CommonFormOfficeUtils.wordUpload(documents, "消防安全走访巡查表模板");
        return vo;
    }

    /**
     * 经营场所消防安全检查记录表
     *
     * @param formSummary
     * @return
     */
    private static ByteArrayOutputStream businessSiteFireSafety(YuHongMenStreetFireHiddenCheckWordDTO formSummary) {
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a2", formSummary.getA2());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("community", formSummary.getCommunity());
        dataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));

        dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.0F));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a15", formSummary.getA15());

        String a21_1 = JSON.parseArray(formSummary.getA21()).getString(0);
        dataMap.put("a21_1_1", "现场/限期/已整改".contains(a21_1) ? "有" : a21_1);
        dataMap.put("a21_1_2", getValue(a21_1));
        String a22_1 = JSON.parseArray(formSummary.getA22()).getString(0);
        dataMap.put("a22_1_1", "现场/限期/已整改".contains(a22_1) ? "否" : a22_1);
        dataMap.put("a22_1_2", getValue(a22_1));
        String a23_1 = JSON.parseArray(formSummary.getA23()).getString(0);
        dataMap.put("a23_1_1", "现场/限期/已整改".contains(a23_1) ? "否" : a23_1);
        dataMap.put("a23_1_2", getValue(a23_1));
        String a24_1 = JSON.parseArray(formSummary.getA24()).getString(0);
        dataMap.put("a24_1_1", "现场/限期/已整改".contains(a24_1) ? "否" : a24_1);
        dataMap.put("a24_1_2", getValue(a24_1));
        String a25_1 = JSON.parseArray(formSummary.getA25()).getString(0);
        dataMap.put("a25_1_1", "现场/限期/已整改".contains(a25_1) ? "是" : a25_1);
        dataMap.put("a25_1_2", getValue(a25_1));
        String a26_1 = JSON.parseArray(formSummary.getA26()).getString(0);
        dataMap.put("a26_1_1", "现场/限期/已整改".contains(a26_1) ? "有" : "无");
        dataMap.put("a26_1_2", getValue(a26_1));
        if ("现场/限期/已整改".contains(a26_1)) {
            String a26_5 = JSON.parseArray(formSummary.getA26()).getString(4);
            dataMap.put("a26_5", a26_5);
        } else {
            dataMap.put("a26_5", null);
        }
        String a27_1 = JSON.parseArray(formSummary.getA27()).getString(0);
        dataMap.put("a27_1_1", "现场/限期/已整改".contains(a27_1) ? "否" : a27_1);
        dataMap.put("a27_1_2", getValue(a27_1));
        String a28_1 = JSON.parseArray(formSummary.getA28()).getString(0);
        dataMap.put("a28_1_1", "现场/限期/已整改".contains(a28_1) ? "否" : a28_1);
        dataMap.put("a28_1_2", getValue(a28_1));
        String a45_4 = JSON.parseArray(formSummary.getA45()).getString(4);
        String a45_1 = JSON.parseArray(formSummary.getA45()).getString(0);
        dataMap.put("a45_5", "");
        dataMap.put("a45_1_1", "");
        dataMap.put("a45_1_2", "");
        if (StrUtil.isBlank(a45_4)) {
            dataMap.put("a45_5", JSON.parseArray(a45_4));
            dataMap.put("a45_1_1", "现场/限期/已整改".contains(a45_1) ? "有" : "无");
            dataMap.put("a45_1_2", getValue(a45_1));
        }

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(a21_1).append(a22_1).append(a23_1).
                append(a24_1).append(a25_1).append(a26_1).append(a27_1).append(a28_1).append(a45_1);

        dataMap.put("yyyy", "");
        dataMap.put("mm", "");
        dataMap.put("dd", "");
        dataMap.put("a7", "");
        if (stringBuilder.toString().contains("限期")) {
            Date a7 = DateUtil.parse(formSummary.getA7(), "yyyy年MM月dd日");
            dataMap.put("yyyy", DateUtil.year(a7));
            dataMap.put("mm", DateUtil.month(a7) + 1);//月份从0开始,所以要加1
            dataMap.put("dd", DateUtil.dayOfMonth(a7));
            dataMap.put("a7", formSummary.getA7());
        }

        return generateWordDocument(dataMap, "yuHong/businessSiteFireSafety.docx",
                "经营场所消防安全检查记录表", System.currentTimeMillis());
    }

    /**
     * 瓶装燃气用户安全检查表
     *
     * @param formSummary
     * @return
     */
    private static ByteArrayOutputStream bottledGasUserSafety(YuHongMenStreetFireHiddenCheckWordDTO formSummary) {
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a2", formSummary.getA2());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("county", formSummary.getCounty());
        dataMap.put("community", formSummary.getCommunity());
        dataMap.put("fillDate", DateUtil.format(formSummary.getFillDate(), "yyyy年MM月dd日"));

        String a6 = formSummary.getA6();
        StringBuilder a6_b = new StringBuilder();
        if (a6.contains("液化石油气")) a6_b.append("①");
        if (a6.contains("城镇燃气用二甲醚")) a6_b.append("②");
        dataMap.put("a6", a6_b.toString());
        String a8 = JSON.parseArray(formSummary.getA8()).getString(0);
        StringBuilder a8_b = new StringBuilder();
        if (a8.contains("正常") || a8.contains("已整改")) a8_b.append("①");
        if (a8.contains("现场") || a8.contains("限期")) {
            if (a8.contains("异常")) a8_b.append("②");
            if (a8.contains("无信息")) a8_b.append("③");
            if (a8.contains("无芯片")) a8_b.append("④");
            if (a8.contains("其他")) a8_b.append("⑤");
        }
        dataMap.put("a8", a8_b.toString());
        dataMap.put("a8_1", (a8.contains("现场") || a8.contains("已整改")) ? "已整改" : a8.contains("限期") ? "整改中" : "");
        String a9 = JSON.parseArray(formSummary.getA9()).getString(0);
        dataMap.put("a9", (a9.contains("现场") || a9.contains("限期") ? "否" : "是"));
        dataMap.put("a9_1", (a9.contains("现场") || a9.contains("已整改")) ? "已整改" : a9.contains("限期") ? "整改中" : "");
        String a10 = JSON.parseArray(formSummary.getA10()).getString(0);
        StringBuilder a10_b = new StringBuilder();
        if (a10.contains("正常") || a10.contains("已整改")) a10_b.append("①");
        if (a10.contains("现场") || a10.contains("限期")) {
            if (a10.contains("长度小于")) a10_b.append("②");
            if (a10.contains("使用三通连接")) a10_b.append("③");
            if (a10.contains("龟")) a10_b.append("④");
            if (a10.contains("泄漏")) a10_b.append("⑤");
            if (a10.contains("其他")) a10_b.append("⑥");
        }
        dataMap.put("a10", a10_b.toString());
        dataMap.put("a10_1", (a10.contains("现场") || a10.contains("已整改")) ? "已整改" : a10.contains("限期") ? "整改中" : "");
        String a11 = JSON.parseArray(formSummary.getA11()).getString(0);
        dataMap.put("a11", "否".contains(a11) ? "否" : "是");
        dataMap.put("a11_1", (a11.contains("现场") || a11.contains("已整改")) ? "已整改" : a11.contains("限期") ? "整改中" : "");
        String a12 = JSON.parseArray(formSummary.getA12()).getString(0);
        StringBuilder a12_b = new StringBuilder();
        if (a12.contains("已安装并正常使用") || a12.contains("已整改")) a12_b.append("①");
        if (a12.contains("限期") || a12.contains("现场")) {
            if (a12.contains("已安装未正常使用")) a12_b.append("②");
            if (a12.contains("未安装")) a12_b.append("③");
            if (a12.contains("其他")) a12_b.append("④");
        }
        dataMap.put("a12", a12_b.toString());
        dataMap.put("a12_1", (a12.contains("现场") || a12.contains("已整改")) ? "已整改" : a12.contains("限期") ? "整改中" : "");
        dataMap.put("a13", formSummary.getA13());

        StringBuilder stringBuilder = new StringBuilder();
        JSONArray jsonArray = JSON.parseArray(formSummary.getA48());
        int i = 0;
        for (int j = 0; j < jsonArray.size(); j++) {
            JSONArray array = jsonArray.getJSONArray(j);
            if ((array.getString(0).contains("现场") && j == jsonArray.size() - 1)
                    || array.getString(0).contains("限期")) {
                i++;
                stringBuilder.append(i + "、");
                stringBuilder.append(array.getString(4));
                if (!stringBuilder.substring(stringBuilder.length() - 1).equals("。")) {
                    stringBuilder.append("。");
                }
            }
        }
        dataMap.put("a14", stringBuilder.toString());

        dataMap.put("yyyy", "");
        dataMap.put("mm", "");
        dataMap.put("dd", "");
        dataMap.put("a7", "");
        if ((a8 + a9 + a10 + a11 + a12).contains("限期")) {
            Date a7 = DateUtil.parse(formSummary.getA7(), "yyyy年MM月dd日");
            dataMap.put("yyyy", DateUtil.year(a7));
            dataMap.put("mm", DateUtil.month(a7) + 1);//月份从0开始,所以要加1
            dataMap.put("dd", DateUtil.dayOfMonth(a7));
            dataMap.put("a7", formSummary.getA7());
        }

        return generateWordDocument(dataMap, "yuHong/bottledGasUserSafety.docx",
                "瓶装燃气用户安全检查表", System.currentTimeMillis());
    }

    private static NiceXWPFDocument businessSiteFireSafetyTemplate(YuHongMenStreetFireHiddenCheckWordDTO formSummary) {
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("a1", null);
        dataMap.put("a2", null);
        dataMap.put("a3", null);
        dataMap.put("a4", null);
        dataMap.put("community", null);
        dataMap.put("fillDate", null);
        dataMap.put("a20", null);
        dataMap.put("a19", null);
        dataMap.put("a15", null);
        dataMap.put("a21_1_1", null);
        dataMap.put("a21_1_2", null);
        dataMap.put("a22_1_1", null);
        dataMap.put("a22_1_2", null);
        dataMap.put("a23_1_1", null);
        dataMap.put("a23_1_2", null);
        dataMap.put("a24_1_1", null);
        dataMap.put("a24_1_2", null);
        dataMap.put("a25_1_1", null);
        dataMap.put("a25_1_2", null);
        dataMap.put("a26_1_1", null);
        dataMap.put("a26_1_2", null);
        dataMap.put("a26_5", null);
        dataMap.put("a27_1_1", null);
        dataMap.put("a27_1_2", null);
        dataMap.put("a28_1_1", null);
        dataMap.put("a28_1_2", null);
        dataMap.put("a45_5", "");
        dataMap.put("a45_1_1", "");
        dataMap.put("a45_1_2", "");
        dataMap.put("yyyy", "");
        dataMap.put("mm", "");
        dataMap.put("dd", "");
        dataMap.put("a7", "");

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, null,
                "yuHong/businessSiteFireSafety.docx");

        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();

        return xwpfDocument;
    }

    /**
     * 瓶装燃气用户安全检查表
     *
     * @param formSummary
     * @return
     */
    private static NiceXWPFDocument bottledGasUserSafetyTemplate(YuHongMenStreetFireHiddenCheckWordDTO formSummary) {
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("a1", null);
        dataMap.put("a2", null);
        dataMap.put("a3", null);
        dataMap.put("a4", null);
        dataMap.put("county", null);
        dataMap.put("community", null);
        dataMap.put("fillDate", null);
        dataMap.put("a6", null);
        dataMap.put("a8", null);
        dataMap.put("a8_1", null);
        dataMap.put("a9", null);
        dataMap.put("a9_1", null);
        dataMap.put("a10", null);
        dataMap.put("a10_1", null);
        dataMap.put("a11", null);
        dataMap.put("a11_1", null);
        dataMap.put("a12", null);
        dataMap.put("a12_1", null);
        dataMap.put("a13", null);
        dataMap.put("a14", null);
        dataMap.put("yyyy", "");
        dataMap.put("mm", "");
        dataMap.put("dd", "");
        dataMap.put("a7", "");

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, null,
                "yuHong/bottledGasUserSafety.docx");

        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();

        return xwpfDocument;
    }

    /**
     * 创建现场照片pdf
     *
     * @param formSummary 现场照片填表数据
     * @return 结果
     */
    private static ByteArrayOutputStream createSitePicture(YuHongMenStreetFireHiddenCheckWordDTO formSummary) {
        if (StringUtils.isNotBlank(formSummary.getA18())) {
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(formSummary.getA18().split(",")));
            String textStr = "附件：{} {} 现场照片";
            textStr = StrFormatter.format(textStr, formSummary.getA1(), DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
            return PictureUtils.getByteArrayOutputStream(textStr, pictureList, "yuHong/pictures.docx");
        }
        return null;
    }

    private static String getValue(String value) {
        if ("现场/已整改".contains(value)) return "已整改";
        if ("限期".contains(value)) return "未整改";
        return null;
    }
}

